package com.clinic.accessor.db;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component;

import com.clinic.entity.db.UserContact;
import com.clinic.entity.param.Index;

@Component
public class UserContactAccessor extends AbstractDbAccessor<UserContact>{
	
	private static final String USER_PREFIX = "user.";
	
	public UserContact getUserContactByContactId(long id) {
		DetachedCriteria criteria = DetachedCriteria.forClass(UserContact.class);
		criteria.add(Restrictions.eq(UserContact.Columns.CONTACT_ID, id));
		return uniqueGeneric(criteria);
	}
	
	public UserContact getUserContactByUserId(long id) {
		DetachedCriteria criteria = DetachedCriteria.forClass(UserContact.class);
		criteria.add(Restrictions.eq(USER_PREFIX.concat(UserContact.Columns.USER_ID), id));
		return uniqueGeneric(criteria);
	}
	
	public List<UserContact> getAllContacts() {
		DetachedCriteria criteria = DetachedCriteria.forClass(UserContact.class);
		criteria.add(Restrictions.gt(UserContact.Columns.CONTACT_ID, 0));
		return listGeneric(criteria, new Index());
	}

}
